function e(e) {
    try {
        var n = wx.getStorageSync("token"), a = wx.getStorageSync("expire"), o = new Date().getTime();
    } catch (e) {}
    n && o < a ? e(n) : (wx.removeStorageSync("token"), wx.removeStorageSync("expire"), 
    wx.login({
        success: function(n) {
            if (n.code) {
                var a = {
                    code: n.code
                };
                wx.request({
                    url: r + "userapi/newlogin",
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: a,
                    method: "POST",
                    success: function(n) {
                        try {
                            1 == n.data.status && (wx.setStorageSync("token", n.data.data.token), wx.setStorageSync("expire", 1e3 * n.data.data.expire), 
                            t(n.data.data.token), e(n.data.data.token));
                        } catch (e) {}
                    }
                });
            }
        }
    }));
}

function t(e) {
    wx.getUserInfo({
        lang: "zh_CN",
        success: function(t) {
            var n = t.userInfo, a = {
                token: e,
                nickname: n.nickName,
                avatarurl: n.avatarUrl,
                gender: n.gender,
                province: n.province,
                city: n.city,
                country: n.country
            };
            wx.request({
                url: r + "userapi/setuserinfo",
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                data: a,
                method: "POST",
                success: function(e) {
                    1 == e.data.status && console.log("更新用户信息");
                }
            });
        }
    });
}

function n(e, t, n, a) {
    wx.request({
        url: r + e,
        header: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        data: n,
        method: t,
        success: function(e) {
            if (2 == e.data.status) try {
                wx.removeStorageSync("token", e.data.data.token), wx.removeStorageSync("expire", e.data.data.expire);
            } catch (e) {} else a(e.data);
        }
    });
}

function a(e, t) {
    var n = new Date(1e3 * parseInt(t)), a = {
        "y+": n.getFullYear(),
        "m+": n.getMonth() + 1,
        "d+": n.getDate(),
        "h+": n.getHours(),
        "i+": n.getMinutes(),
        "s+": n.getSeconds()
    };
    for (var o in a) new RegExp("(" + o + ")").test(e) && (e = e.replace(RegExp.$1, a[o] > 9 ? a[o] : "0" + a[o]));
    return e;
}

var o = function(e) {
    return (e = e.toString())[1] ? e : "0" + e;
}, r = getApp().globalData.url.apiUrl;

module.exports = {
    formatTime: function(e) {
        var t = e.getFullYear(), n = e.getMonth() + 1, a = e.getDate(), r = e.getHours(), c = e.getMinutes(), i = e.getSeconds();
        return [ t, n, a ].map(o).join("/") + " " + [ r, c, i ].map(o).join(":");
    },
    getToken: e,
    sendrequest: n,
    upload: function(e, t, n, a, o) {
        wx.uploadFile({
            url: r + e,
            filePath: t,
            name: n,
            formData: a,
            success: function(e) {
                console.log(e);
                var t = JSON.parse(e.data);
                o(t);
            }
        });
    },
    date: a,
    malert: function(e) {
        wx.showToast({
            title: e,
            icon: "loading",
            duration: 2e3
        });
    },
    filter: function(e) {
        for (var t = Date.parse(new Date()), n = 0; n < e.length; n++) t > 1e3 * e[n].apply_endtime ? e[n].endstring = "已经结束报名" : e[n].endstring = a("m月d日", e[n].apply_endtime) + "后截止报名";
        return e;
    },
    formatUS: function(e) {
        for (var t, n, a, o = e.split("&"), r = [], c = 0; c < o.length; c++) (a = o[c].indexOf(":")) > 0 && (t = o[c].substring(0, a), 
        n = o[c].substr(a + 1), r[t] = n);
        return r;
    },
    addcollect: function(t) {
        e(function(e) {
            n("posterapi/addcollect", "POST", {
                token: e,
                id: t
            }, function(e) {
                if (1 == e.status) return wx.showToast({
                    title: "已收藏",
                    icon: "success",
                    duration: 2e3
                }), !0;
            });
        });
    },
    getuserInfo: t
};